//
//  BaseViewController.m
//  专注
//
//  Created by 专注 on 2021/3/11.
//  Copyright © 2021 专注. All rights reserved.
//
#import "BaseViewController.h"
#import  <objc/runtime.h>
#import "UIImage+Color.h"

@interface BaseViewController (){
    NSMutableArray *_mainViewControllers;
    //初次加载的数据刷新判定
    BOOL _isInitData;
    //每次展示时的数据刷新判定
    BOOL _initReloadData;
    
}
@property(nonatomic,strong) BaseNavigationBar *navigationBarView;

@end

@implementation BaseViewController

//视图控制器中的视图加载完成，viewController自带的view加载完成
- (void)viewDidLoad {
//    NSLog(@"%s", __FUNCTION__);
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
    [self initNavigation];
    [self loadViewIfNeeded];
}
/** 子控制器初始化视图 */
-(void)initView{}

- (void)viewWillAppear:(BOOL)animated {
//    NSLog(@"视图将要出现：%@", self.class);
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillLayoutSubviews {
//    NSLog(@"view 即将布局其 Subviews：%@", self.class);
    [super viewWillLayoutSubviews];
}
- (void)viewDidLayoutSubviews {
//    NSLog(@"view 已经布局其 Subviews：%@", self.class);
    [super viewDidLayoutSubviews];
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(runInitData) object:@"runInitData"];
    [self performSelector:@selector(runInitData) withObject:@"runInitData" afterDelay:.0f];
}
-(void)runInitData{
    if (!_isInitData){
        _isInitData = YES;
        [self initData];
    }
}
- (void)viewDidAppear:(BOOL)animated {
//    NSLog(@"视图已经出现：%@", self.class);
    [super viewDidAppear:animated];
    if (_initReloadData){
        [self initReloadData];
    }
    _initReloadData = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
//    NSLog(@"视图将要消失：%@", self.class);
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidDisappear:(BOOL)animated {
//    NSLog(@"视图已经消失：%@", self.class);
    [super viewDidDisappear:animated];
    if (![self.navigationController.viewControllers containsObject:self]) {
        [self releseViewController];
    }
}
//模拟内存警告:点击模拟器->hardware-> Simulate Memory Warning
- (void)didReceiveMemoryWarning {
    NSLog(@"出现内存警告：%@", self.class);
    [super didReceiveMemoryWarning];
}

/** 子控制第一次加载数据了 */
-(void)initData{}
/** 子控制每次展示页面刷新数据 */
-(void)initReloadData{}
/************************************************************
 *     延迟加载
 ************************************************************/
#pragma mark --- Navigation
-(void)initNavigation{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:UIColor.clearColor] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:UIColor.clearColor]];
    [self.view addSubview:self.navigationBarView];
    [self.navigationBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view);
        make.height.mas_offset(kNavBarAndStatusBarHeight);
    }];
    [self initTitle];
    [self initBarBurton];
}
-(void)setNavigationColor:(UIColor *)color navigationLienColor:(UIColor *)lienColor{
    [self.navigationBarView setNavigationColor:color navigationLienColor:lienColor];
}
#pragma mark --- titleView
-(void)initTitle{
    if ([self respondsToSelector:@selector(set_title)]) {
        NSString *title = [self set_title];
        [self set_title:title];
    }
    if ([self respondsToSelector:@selector(set_titleView)]) {
        UIView *view = [self set_titleView];
        [self set_titleView:view];
    }
}
-(void)set_title:(NSString *)title{
    if (!title) {
        return;
    }
    UILabel *label = [[UILabel alloc]init];
    label.numberOfLines=0;//可能出现多行的标题
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.userInteractionEnabled = YES;
    label.text = title;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleClick:)];
    [label addGestureRecognizer:tap];
    [self.navigationBarView titileLabel:label];
}
-(void)set_titleView:(UIView *)titleView{
    [self.navigationBarView titleView:titleView];
}
-(void)titleClick:(UIGestureRecognizer*)Tap{
    UIView *view = Tap.view;
    if ([self respondsToSelector:@selector(title_click_event:)]) {
        [self title_click_event:view];
    }
}
#pragma mark -- init_button
-(void)initBarBurton{
    [self leftButtonArr];
    [self rightButtonArr];
    if (![self leftButton]) {
        [self configLeftBaritemWithImage];
    }
    if (![self rightButton]) {
        [self configRightBaritemWithImage];
    }
}

#pragma mark -- 默认
-(UIButton *)set_leftButton{
    if (self.navigationController.viewControllers.count > 1) {
        UIButton *button = [[UIButton alloc]init];
        [button setImage:[UIImage svgName:@"public_back_icon"] forState:UIControlStateNormal];
        return button;
    }
    return nil;
}
-(void)left_button_event:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark -- left_button
-(BOOL)leftButton{
    BOOL isleft =  [self respondsToSelector:@selector(set_leftButton)];
    if (isleft) {
        UIButton *leftbutton = [self set_leftButton];
        if (!leftbutton){
            return NO;
        }
        [leftbutton addTarget:self action:@selector(left_click:) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationBarView leftBarButtonItems:@[leftbutton]];
    }
    return isleft;
}
-(BOOL)leftButtonArr{
    BOOL isleft =  [self respondsToSelector:@selector(set_leftButtonArr)];
    if (isleft) {
        NSArray *left_buttons = [self set_leftButtonArr];
        for (UIButton *btn in left_buttons) {
            [btn addTarget:self action:@selector(right_click:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self.navigationBarView leftBarButtonItems:left_buttons];
    }
    return isleft;
}
-(void)configLeftBaritemWithImage{
    if ([self respondsToSelector:@selector(set_leftBarButtonItemWithImage)]) {
        UIImage *image = [[self set_leftBarButtonItemWithImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIButton *leftbutton = [[UIButton alloc]init];
        [leftbutton setImage:image forState:UIControlStateNormal];
        [leftbutton addTarget:self action:@selector(left_click:) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationBarView leftBarButtonItems:@[leftbutton]];
    }
}
-(void)left_click:(id)sender{
    if ([self respondsToSelector:@selector(left_button_event:)]) {
        [self left_button_event:sender];
    }
}
#pragma mark -- right_button
-(BOOL)rightButton{
    BOOL isright = [self respondsToSelector:@selector(set_rightButton)];
    if (isright) {
        UIButton *right_button = [self set_rightButton];
        [right_button addTarget:self action:@selector(right_click:) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationBarView rightBarButtonItems:@[right_button]];
    }
    return isright;
}
-(BOOL)rightButtonArr{
    BOOL isright = [self respondsToSelector:@selector(set_rightButtonArr)];
    if (isright) {
        NSArray *right_buttons = [self set_rightButtonArr];
        for (UIButton *btn in right_buttons) {
            [btn addTarget:self action:@selector(right_click:) forControlEvents:UIControlEventTouchUpInside];
        }
        [self.navigationBarView rightBarButtonItems:right_buttons];
    }
    return isright;
}
-(void)configRightBaritemWithImage{
    if ([self respondsToSelector:@selector(set_rightBarButtonItemWithImage)]) {
        UIImage *image = [[self set_rightBarButtonItemWithImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIButton *rightbutton = [[UIButton alloc]init];
        [rightbutton setImage:image forState:UIControlStateNormal];
        [rightbutton addTarget:self action:@selector(right_click:) forControlEvents:UIControlEventTouchUpInside];
        [self.navigationBarView rightBarButtonItems:@[rightbutton]];
    }
}
-(void)right_click:(id)sender{
    if ([self respondsToSelector:@selector(right_button_event:)]) {
        [self right_button_event:sender];
    }
}


-(BaseNavigationBar *)navigationBarView{
    if (!_navigationBarView) {
        _navigationBarView = [[BaseNavigationBar alloc]init];
        _navigationBarView.backgroundColor = UIColor.whiteColor;
    }
    return _navigationBarView;
}
-(UIView *)contentView{
    if (!_contentView){
        _contentView = [[UIView alloc]init];
        [self.view addSubview:_contentView];
        [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(self.view);
            make.top.mas_equalTo(self.view).mas_offset(kNavBarAndStatusBarHeight);
        }];
    }
    return _contentView;
}
-(UIView *)contentFullView{
    if (!_contentFullView){
        _contentFullView = [[UIView alloc]init];
        [self.view addSubview:_contentFullView];
        [_contentFullView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(self.view);
            make.top.mas_equalTo(self.view);
        }];
    }
    return _contentFullView;
}
/** 控制器主动销毁的主动清理 */
-(void)releseViewController{}
-(void)dealloc{
    NSLog(@"\n----------------------------%@--销毁-----------------------\n",self.class);
}
@end


